//
//  QCCustomController.swift
//  爱汽车
//
//  Created by tucici on 2023/8/27.
//

import UIKit

class QCCustomController: QCViewController {
    var prepos : PrePos?
    private var currentBytes = [UInt8]()
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    
    override func initUI() {
        title = prepos?.name ?? ""
        backBtn?.isHidden = true
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(QCSetupTopCell.nib(), forCellReuseIdentifier: QCSetupTopCell.ID())
        tableView.register(QCCustomCell.nib(), forCellReuseIdentifier: QCCustomCell.ID())
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        BLEData.appSetDataToPer(AIQC.userInfo.cmd,"00").write
    }
    
    override var base_info: [UInt8]{
        didSet{
            currentBytes = base_info
            tableView.reloadData()
        }
    }
    override var bytes: [UInt8]{
        didSet{
            currentBytes = bytes
            tableView.reloadData()
        }
    }
    
    func action(_ title:String, _ index:Int){
        switch title{
        case LS("退出"):
            backBtnAction()
        case LS("保存"):
            let hex = HEX(UInt8(prepos?.num ?? 0))
            BLEData.appSetDataToPer(AIQC.setPreHight.cmd, hex).write
            backBtnAction()
        default:
            break
        }
    }
}

//MARK: - UITableViewDataSource
extension QCCustomController : UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0{
            let cell = tableView.dequeueReusableCell(withIdentifier: QCSetupTopCell.ID()) as! QCSetupTopCell
            cell.setupView.style = .custom(1)
            cell.setupView.customDangwei = (prepos?.num ?? 0) - 1
            cell.bytes = currentBytes
            return cell
        }else{
            let cell = tableView.dequeueReusableCell(withIdentifier: QCCustomCell.ID()) as! QCCustomCell
            cell.block = {[weak self] text in
                self?.action(text, indexPath.row)
            }
            return cell
        }
    }
}
//MARK: - UITableViewDelegate
extension QCCustomController : UITableViewDelegate
{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0{
            let y = (kUIScreenHeight - KUITabBarHeight - kUIStatusBarHeight - 192)
            QCSetupTopCellH = max(y, 436)
            QCSetupTopCellH = min(QCSetupTopCellH, 536)
            return QCSetupTopCellH
        }
        return 140
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}
